package com.ziq.baselib.test.page

import android.os.Bundle
import android.util.Log
import android.view.ViewGroup
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.ziq.base.base.BaseActivity
import com.ziq.base.extension.BindingViewHolder
import com.ziq.base.extension.binding
import com.ziq.base.extension.click
import com.ziq.base.extension.createBindingViewHolder
import com.ziq.base.view.recycleview.BaseListAdapter
import com.ziq.baselib.databinding.ActivityBaseListAdapterBinding
import com.ziq.baselib.databinding.ItemBaseListTestBinding

class BaseListAdapterActivity: BaseActivity() {

    private val binding : ActivityBaseListAdapterBinding by binding()

    var adapter : Adapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        adapter = Adapter()
        binding.recyclerView.layoutManager = LinearLayoutManager(this)
        binding.recyclerView.adapter = adapter

        binding.add.click {
            adapter?.let {
                val targetData = mutableListOf<TestData>().apply {
                    this.addAll(it.getCurrentList())
                    val count = adapter?.itemCount?:0
                    for (index in 0 .. 10) {
                        val targetIndex = index + count
                        add(TestData("$targetIndex", "name$targetIndex", targetIndex))
                    }
                }
                it.submitList(targetData)
            }
        }

        binding.remove.click {
            adapter?.let {
                val targetData = mutableListOf<TestData>().apply {
                    this.addAll(it.getCurrentList())
                }
                targetData.removeAt(0)
                it.submitList(targetData)
            }
        }

        binding.update.click {
            adapter?.let {
                val targetData = mutableListOf<TestData>().apply {
                    this.addAll(it.getCurrentList())
                }
                //有效 不同对象
                targetData.set(0, targetData.get(0).copy().apply {
                    name = "test0"
                })
                //无效 同一个对象的引用，一并修改了，判断不出,使用hasChanged 去修改
                targetData.get(3).apply {
                    name = "test3"
                    hasChanged = true
                }
                it.submitList(targetData)
            }
        }

        binding.update2.click {
            adapter?.let {
                val targetData = mutableListOf<TestData>().apply {
                    this.addAll(it.getCurrentList())
                }
                it.submitList(targetData)
            }
        }

    }

    data class TestData(var id : String, var name : String, var age : Int) : BaseListAdapter.BaseListAdapterData {
        var hasChanged = false
        override fun hasContentChanged(): Boolean {
            return hasChanged
        }

        override fun resetContentChanged() {
            hasChanged = false
        }
    }

    class Adapter : BaseListAdapter<TestData, ItemBaseListTestBinding>(){
        override fun areItemsTheSame(oldItem: TestData, newItem: TestData): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: TestData, newItem: TestData): Boolean {
            val resutl = oldItem.id == newItem.id && oldItem.name == newItem.name && oldItem.age == newItem.age
            Log.i("wyq", "BaseListAdapter areContentsTheSame $resutl ${oldItem} ${newItem}")
            return resutl
         }

        override fun onCreateViewHolder(
            parent: ViewGroup,
            viewType: Int
        ): BindingViewHolder<ItemBaseListTestBinding> {
            return createBindingViewHolder(parent)
        }

        override fun onBindViewHolder(
            holder: BindingViewHolder<ItemBaseListTestBinding>,
            position: Int
        ) {
            Log.i("wyq", "BaseListAdapter onBindViewHolder $position")
            getItem(position).let {
                holder.binding.text.text = "${it.id}-${it.name}-${it.age}"
                holder.itemView.click {view ->
                    Toast.makeText(view.context, "${it.id}-${it.name}-${it.age}", Toast.LENGTH_SHORT).show()
                }
            }
        }

    }

}